﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL_V1.App_Code.DBBroker;
using EntityObjects;
namespace BAL.Controller
{
    public class RaisePurchaseOrderControl
    {
        CatalogueDBBroker cDB;
        SupplierDBBroker sDB;
        PurchaseOrderDBBroker poDB;
        public RaisePurchaseOrderControl()
        {
            cDB = CatalogueDBBroker.getCatalogueDBBroker();
            sDB = SupplierDBBroker.getSupplierDBBroker();
            poDB = PurchaseOrderDBBroker.getPurchaseOrderDBBroker();
        }

        public List<Catalogue> getReorderLvlPurchaseOrder()
        {
            return cDB.getCatalogueBelowReOrderLevel();
        }

        public Catalogue getReorderLvlPurchaseOrderById(string ItemId)
        {
            List<Catalogue> lst = new List<Catalogue>();
            lst = cDB.getCatalogueBelowReOrderLevel();
            foreach (Catalogue c in lst)
            {
                if (c.ItemId == ItemId) return c;
            }
            return null;
        }

        public List<SupplierTender> getSupplierTender(string ItemId)
        {
            List<SupplierTender> lst = new List<SupplierTender>();
            List<SupplierTender> temp = new List<SupplierTender>();
            lst = sDB.selectAllSupplierTender();
            foreach (SupplierTender s in lst)
            {
                if (s.ItemId == ItemId)
                {
                    temp.Add(s);
                }
            }
            return temp;
        }

        public string getDescription(string id)
        {
            Catalogue c = cDB.selectCatalogue(id);
            return c.Description;

        }

        public string getPurchaseOrderNo()
        {
            List<PurchaseOrder> lst = poDB.selectAllPurchaseOrder();
            if (lst.Count != 0)
            {
                PurchaseOrder po = lst.Last();
                return po.PONo;
            }
            return null;
        }

        public void Insert(PurchaseOrder po)
        {
            poDB.addPurchaseOrder(po);
            foreach (PurchaseOrderDetails p in po.PoDetails_list)
            {
                p.PONo = po.PONo;
                poDB.addPurchaseOrderDetails(p);
            }
        }

        public bool FindPurchasedItem(string id)
        {
            bool flag = false;
            List<PurchaseOrderDetails> poDList = poDB.getPurchaseItemByPurchasedStatus();
            if (poDList != null)
            {
                foreach (PurchaseOrderDetails t in poDList)
                {
                    if (t.ItemId == id) flag = true;
                }
            }
            return flag;
        }
    }
}
